#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>

#include "debug.h"
#include "mav.h"
#include "mav_db.h"

static int runflag = 1;
struct log_module log_conf = 
{ 	
    .log_path = "mav.log",
    .max_size = 512*1024,
    .fp = NULL,
    .mutex = PTHREAD_MUTEX_INITIALIZER,
};
void sig_handle(int signo)
{
    if(signo == SIGINT)
	runflag = 0;
}
#define MAV_TEST_FILE "sd1/20141111/111111.mav"
unsigned char buf[4096]={0};
static int test_mav_file(void)
{
    int ret = 0,i = 0;
    //printf("---mav_header_list = %lu\n",sizeof(struct mav_header_list));
    MAV_HANDLE pmav = mav_init_handle(MAV_TEST_FILE,MAV_STREAM_VAMIX);
    if(pmav==NULL)
    {
	printf("mav_init_handle fail!\n");
	exit(EXIT_FAILURE);
    }

    struct mav_frame_header header;
    unsigned char frame[1024]={0};
    header.type = MAV_FRAME_ISLICE;
    header.len = sizeof(frame);
    header.pts = 0x12345678;
    header.fts = time(NULL);
    printf("---start time = %ld,header size = %lu\n",header.fts,sizeof(header));
    int buf_len = sizeof(header)+sizeof(frame);
    while(runflag&&i++<10)
    {
	header.fts = time(NULL);
	memset(frame,i%255,sizeof(frame));
	memcpy(buf,&header,sizeof(header));
	memcpy(buf+sizeof(header),frame,sizeof(frame));
	
	ret = mav_write_packet(pmav,MAV_STREAM_VIDEO,buf,buf_len);
	if(ret!=0)
	{
	    printf("mav_write_packet error,ret = %d\n",ret);
	    break;
	}
	sleep(1);
    }
    mav_exit_handle(pmav);
    printf("---end time = %ld\n",time(NULL));

    ret = mav_fix_handle(MAV_TEST_FILE);
    printf("mav_fix_handle ret = %d\n",ret);

    time_t start = 0,end = 0;
    ret = mav_get_tszone(MAV_TEST_FILE,&start,&end);
    printf("mav_get_tszone ret = %d,start = %ld,end = %ld\n",ret,start,end);

    FILE *fp = mav_seek_frame(MAV_TEST_FILE,start+5);
    if(fp==NULL)
    {
	printf("mav_seek_frame tnode = %ld fail!\n",start+20);
	exit(EXIT_FAILURE);
    }
    else
    {
	printf("seek [%lds] offset = %ld\n",start+20,ftell(fp));
	fclose(fp);
    }
    return ret;
}

static int test_mav_db(void)
{
    int ret = 0,i = 0;

    i = 0;
    while(runflag&&i++<50)
    {
	ret = mav_db_add_item("sd1/20141110/162311.mav");
	ret = mav_db_add_item("sd1/20141111/162311.mav");
	if(ret < 0)
	{
	    printf("mav_db_add_item ret = %d\n",ret);
	    break;
	}
    }
   // mav_db_print_list();
    i = 0;
    while(runflag&&i++<50)
    {
        ret = mav_db_del_item("sd1/20141110/162311.mav");
        ret = mav_db_del_item("sd1/20141111/162311.mav");
        if(ret < 0)
        {
            printf("mav_db_del_item ret = %d\n",ret);
            break;
        }
    }
    //mav_db_print_list();
#if 0
    time_t tstart = 1415623983,tend;
    FILE *fp = mav_db_get_file(tstart,&tend);
    if(fp==NULL)
    {
	printf("mav_db_get_file tnode = %ld fail!\n",tstart);
	exit(EXIT_FAILURE);
    }
    else
    {
	printf("seek [%lds] offset = %ld\n",tstart,ftell(fp));
	fclose(fp);
    }
#endif

    return ret;
}

int main(int argc, char *argv[])
{
    int ret = 0,i = 0;

    signal(SIGINT,sig_handle);

    ret = mav_db_init_list();
    if(ret < 0)
    {
	printf("mav_db_init_list fail,ret = %d\n",ret);
	exit(EXIT_FAILURE);
    }
    mav_db_print_list();

    ret = test_mav_file();
    //ret = test_mav_db();

    mav_db_print_list();
    mav_db_exit_list();
    return ret;
}

